﻿using System.IO;
using System.IO.Compression;
using System.Text;

namespace PmfWeb.Core.Forms
{
    ///<summary>
    /// 前台页面基类
    ///</summary>
    public class PageBase :System.Web.UI.Page
    {
        protected bool isCompressViewState = false;

        /// <summary>
        /// 向页面添加js脚本
        /// </summary>
        /// <param name="js">js脚本内容</param>
        /// <returns>脚本字符串</returns>
        protected string AddScript(string js)
        {
            StringBuilder script = new StringBuilder("");
            script.AppendFormat("\r\n<script type=\"text/javascript>\r\n{0}</script>", js);
            return script.ToString();
        }
        

        #region 压缩方法
        public static byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            GZipStream stream = new GZipStream(ms, CompressionMode.Compress);
            stream.Write(data, 0, data.Length);
            stream.Close();
            return ms.ToArray();
        }
        #endregion

        #region 解压方法
        public static byte[] Decompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(data, 0, data.Length);
            ms.Position = 0;
            GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
            MemoryStream temp = new MemoryStream();
            byte[] buffer = new byte[1024];
            while (true)
            {
                int read = stream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                {
                    break;
                }
                else
                {
                    temp.Write(buffer, 0, buffer.Length);
                }
            }
            stream.Close();
            return temp.ToArray();
        }
        #endregion


        /// <summary>
        /// 本页请求的参数,如defalt.aspx?id = 1,返回id=1
        /// </summary>
        /// <returns></returns>
        protected string GetQueryParas()
        {
            return this.ClientQueryString;
        }

        //#region 序列化
        //protected override void SavePageStateToPersistenceMedium(object state)
        //{
        //    if(!isCompressViewState)
        //    {
        //        base.SavePageStateToPersistenceMedium(state);
        //        return;
        //    }
        //    LosFormatter formatter = new LosFormatter();
        //    StringWriter writer = new StringWriter();
        //    formatter.Serialize(writer, state);
        //    string viewState = writer.ToString();
        //    string str = "";
        //    if (viewState.Length > 200)
        //    {
        //        byte[] data = Convert.FromBase64String(viewState);
        //        byte[] compressedData = Compress(data);
        //        str = Convert.ToBase64String(compressedData);
        //    }
        //    else
        //    {
        //        str = viewState;
        //    }
        //    ClientScript.RegisterHiddenField("__MYVIEWSTATE", str);
        //}

        //#endregion

        //#region 反序列化
        //protected override object LoadPageStateFromPersistenceMedium()
        //{
        //    if (isCompressViewState)
        //    {
        //       string viewstate = Request.Form["__MYVIEWSTATE"];
        //        string str = "";
        //        if (viewstate.Length > 200)
        //        {
        //            byte[] data = Convert.FromBase64String(viewstate);
        //            byte[] uncompressedData = Decompress(data);
        //            str = Convert.ToBase64String(uncompressedData);
        //        }
        //        else
        //        {
        //            str = viewstate;
        //        }
        //        LosFormatter formatter = new LosFormatter();
        //        return formatter.Deserialize(str);                 
        //    }
        //    return base.LoadPageStateFromPersistenceMedium();
        //}
        //#endregion
    }
}